package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IAIRecordDao;
import com.qymerge.manage.base.ro.AIRecordRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.*;

/**
 *<p>Title: AIRecordDaoImpl</p>
 *<p>Description: AI客服</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 17:54
 */
@Repository
public class AIRecordDaoImpl extends BaseDao implements IAIRecordDao {

    /**
     * 查询所有分页
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public List list(AIRecordRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = ParamKey.NameSpace.TableSelect.airecord;
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql += " and id=:id";
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getTitle())) {
            sql += " and title like :title";
            paramMap.put("title", "%" + ro.getTitle() + "%");
        }

        if (StringUtils.isNotBlank(ro.getSearchkey())) {
            sql += " and searchkey like :searchkey";
            paramMap.put("searchkey", "%" + ro.getSearchkey() + "%");
        }

        if (StringUtils.isNotBlank(ro.getInfo())) {
            sql += " and info like :info";
            paramMap.put("info", "%" + ro.getInfo() + "%");
        }

        // 分页数据获取
        sql += " order by id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return jdbc.query(sql, paramMap, new BeanPropertyRowMapper<AIRecordRO>(AIRecordRO.class));
    }

    /**
     * 所有数量
     *
     * @param
     * @return
     */
    @Override
    public int total(AIRecordRO ro) {
        String sql = "select count(1) from airecord where 1=1";
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (ro.getId() > 0) {
            sql += " and id=:id";
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getTitle())) {
            sql += " and title like :title";
            paramMap.put("title", "%" + ro.getTitle() + "%");
        }

        if (StringUtils.isNotBlank(ro.getSearchkey())) {
            sql += " and searchkey like :searchkey";
            paramMap.put("searchkey", "%" + ro.getSearchkey() + "%");
        }

        if (StringUtils.isNotBlank(ro.getInfo())) {
            sql += " and info like :info";
            paramMap.put("info", "%" + ro.getInfo() + "%");
        }

        return jdbc.queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public AIRecordRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(ParamKey.NameSpace.TableSelect.airecord);

        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (id > 0) {
            sql.append(" and id=:id");
            paramMap.put("id", id);
        }
        return jdbc.queryForObject(sql.toString(), paramMap, new BeanPropertyRowMapper<AIRecordRO>(AIRecordRO.class));
    }

    @Override
    public List<AIRecordRO> findAll(AIRecordRO ro) {
        StringBuffer sql = new StringBuffer(ParamKey.NameSpace.TableSelect.airecord);
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql.append(" and id=:id");
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getTitle())) {
            sql.append(" and cardname like :cardname");
            paramMap.put("cardname", "%" + ro.getTitle() + "%");
        }

        if (StringUtils.isNotBlank(ro.getSearchkey())) {
            sql.append(" and searchkey like :searchkey");
            paramMap.put("searchkey", "%" + ro.getSearchkey() + "%");
        }

        if (StringUtils.isNotBlank(ro.getInfo())) {
            sql.append(" and info like :info");
            paramMap.put("info", "%" + ro.getInfo() + "%");
        }

        sql.append(" order by id desc");
        return jdbc.query(sql.toString(), paramMap, new BeanPropertyRowMapper<AIRecordRO>(AIRecordRO.class));
    }

    @Override
    public int install(AIRecordRO ro) {
        StringBuilder sql = new StringBuilder("insert into airecord(title,searchkey,info) values(?,?,?)");

        List params = new ArrayList();
        params.add(ro.getTitle());
        params.add(ro.getSearchkey());
        params.add(ro.getInfo());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(AIRecordRO ro) {
        StringBuilder sql = new StringBuilder("update airecord set title=?,searchkey=?,info=? ");

        List params = new LinkedList();
        params.add(ro.getTitle());
        params.add(ro.getSearchkey());
        params.add(ro.getInfo());

        sql.append("where id=?");
        params.add(ro.getId());
        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int remove(List<Long> ids) {
        // 删除
        String sql="delete from airecord where id=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };
        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }


}
